﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        ManagementContract dt = new ManagementContract();
        DataSet ds = dt.GetContractFeasibility();
        GridView1.DataSource = ds;
        GridView2.DataSource = dt.GetAllContract();
        DataBind();
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ManagementContract dt = new ManagementContract();
        if (dt.ActiveContract(GridView1.SelectedDataKey[0]))
        {
            Response.Write("Success");
            LoadData();
        }
        else
        {
            Response.Write("False");
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ManagementContract dt = new ManagementContract();
        dt.NotFeasibility(Int32.Parse(e.Keys[0].ToString()));
        LoadData();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Response.Redirect("FormContract.aspx?ContractID=" + e.Keys[0]);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(DropDownList1.SelectedValue);
        ManagementContract mcon = new ManagementContract();
        DataSet ds = new DataSet();
        if (DropDownList1.SelectedValue.Equals("ContractID"))
        {
            ds = mcon.SearchByContractID(txtSearch.Text);
        }
        else if (DropDownList1.SelectedValue.Equals("CustomerID"))
        {
            ds = mcon.SearchByCustomerID(txtSearch.Text);
        }
        else if (DropDownList1.SelectedValue.Equals("CustomerName"))
        {
            ds = mcon.SearchByCustomerName(txtSearch.Text);
        }
        else
        {
            Response.Write("false");
        }
        GridView2.DataSource = ds;
        GridView2.DataBind();
    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("FormContract.aspx?ContractID=" + GridView2.SelectedDataKey[0]);
    }
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ManagementContract dt = new ManagementContract();
        dt.NotFeasibility(Int32.Parse(e.Keys[0].ToString()));
        LoadData();
    }
}